package org.itboys.product.web;

import com.google.common.collect.Lists;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.product.entity.Category;
import org.itboys.product.entity.ProductCategory;
import org.itboys.product.service.ProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 商品分类
 * @author weisky
 *
 */
@RestController
@RequestMapping("/product/category")
public class ProductCategoryController extends BaseController{

	@Autowired
	private ProductCategoryService productCategoryService;
	@RequestMapping("/toListVM")
	public ModelAndView toListVM(HttpServletRequest request, HttpServletResponse response) {
		return new ModelAndView("/ace/product/category-list");
	}
	@RequestMapping("/list")
	public void list(HttpServletRequest request,HttpServletResponse response){
		List<ProductCategory> list = productCategoryService.list(request);
		JsonPageUtils.renderJsonPage(list!=null&&list.size()>0?list.size():0L, list, response);
	}
	
	@RequestMapping("/getById")
	public void getById(@RequestParam Long id,
			HttpServletRequest request,HttpServletResponse response){
		AjaxUtils.renderJson(response, productCategoryService.getById(id));
	}
	
	@RequestMapping("/save")
	public void save(ProductCategory productCategory,
			@RequestParam(value="tlogo",required=false) MultipartFile tlogo,
			@RequestParam(value="tImage",required=false) MultipartFile tImage,
			HttpServletRequest request,HttpServletResponse response){
		boolean result = productCategoryService.insert(productCategory, tlogo, tImage);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}

	/**
	 * 以树的形式获取分类
	 * @param response
     */
	@RequestMapping("/trees")
	public void trees(@RequestParam("sellerId")Long sellerId, HttpServletResponse response){
		Category[] categories = productCategoryService.getCategoryTree(sellerId);
		//List<EasyUiTreeDO> trees = Lists.newArrayListWithExpectedSize(categories.length);
		for (Category category : categories) {
			/*EasyUiTreeDO treeDO = new EasyUiTreeDO();
			treeDO.setId(category.getId());
			treeDO.setText(category.getName());
			List<EasyUiTreeDO> children = Lists.newArrayList();
			for (Category child : category.getChildren()) {
				EasyUiTreeDO chDO = new EasyUiTreeDO();
				chDO.setId(child.getId());
				chDO.setText(child.getName());
				children.add(chDO);
			}
			treeDO.setChildren(children);
			trees.add(treeDO);*/
		}
		//AjaxUtils.renderJson(response, trees);
	}
	
	@RequestMapping("/delete")
	public void delete(@RequestParam Long id,
			HttpServletRequest request,HttpServletResponse response){
		boolean result = productCategoryService.dele(id);
		if(result){
			AjaxUtils.renderText(response, CommonConstants.SUCCESS);
		}else{
			AjaxUtils.renderText(response, CommonConstants.FAIL);
		}
	}
}
